package com.jike.noobmoney.mvp.view.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.jike.noobmoney.R;
import com.jike.noobmoney.adapter.ChannelsAdapter;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.entity.MyTaskChannelEntity;
import com.jike.noobmoney.mvp.presenter.TaskPresenter;
import com.jike.noobmoney.mvp.view.fragment.ShenHeFragment;
import com.jike.noobmoney.mvp.view.fragment.ShenShuFragment;
import com.jike.noobmoney.mvp.view.widget.DownloadDialog;
import com.jike.noobmoney.net.IView;
import com.jike.noobmoney.util.Logger;
import com.jike.noobmoney.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 审核页面
 * 注:我的任务/我的发布/审核页面都用同一个layout
 */
public class ShenHeActivity extends BaseActivity implements IView {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.channelTab)
    TabLayout channelTab;
    @BindView(R.id.viewpger)
    ViewPager viewpger;
    @BindView(R.id.tv_right)
    TextView tvRight;

    private List<MyTaskChannelEntity> tabNameList = new ArrayList<>();
    private List<Fragment> newsFragmentList = new ArrayList<>();

    public static final String t_id = "t_id";
    private String taskId;
    private TaskPresenter taskPresenter;

    public static void startAct(Context context, String t_id) {
        Intent intent = new Intent(context, ShenHeActivity.class);
        intent.putExtra(ShenHeActivity.t_id, t_id);
        context.startActivity(intent);
    }

    @Override
    protected int layoutResID() {
        return R.layout.activity_my_shenhe_task;
    }

    @Override
    protected void initData() {
        if (getIntent() != null) {
            taskId = getIntent().getStringExtra(ShenHeActivity.t_id);
        }
        taskPresenter = new TaskPresenter(this);
        getData();
    }
    /**
     * 如果要全屏,重写这个方法返回false
     */
    public Boolean isfits() {
        return true;
    }

    /**
     * 如果要白色主题,重写这个方法返回false
     */
    public boolean ismain() {
        return true;
    }
    /**
     * 获取任务详情
     */
    private void getData() {
        tvTitle.setText("审核");
        tvRight.setText("下载图片");
        tvRight.setTextColor(Color.WHITE);
        tvRight.setVisibility(View.VISIBLE);
        tabNameList.add(new MyTaskChannelEntity("待完成", "1"));
        tabNameList.add(new MyTaskChannelEntity("待审核", "3"));
        tabNameList.add(new MyTaskChannelEntity("已通过", "4"));
        tabNameList.add(new MyTaskChannelEntity("未通过", "2"));
        tabNameList.add(new MyTaskChannelEntity("申诉", "5"));

        for (int i = 0; i < tabNameList.size(); i++) {
            ShenHeFragment newsListFragment = new ShenHeFragment();
            Bundle bundle = new Bundle();
            bundle.putString(ShenHeFragment.Channels, tabNameList.get(i).getStatus());
            bundle.putString(ShenHeFragment.t_id, taskId);
            newsListFragment.setArguments(bundle);
            newsFragmentList.add(newsListFragment);
            channelTab.addTab(channelTab.newTab().setText(tabNameList.get(i).getTitle()));
        }
//        ShenShuFragment shenShuFragment=new ShenShuFragment();
//        Bundle bundle = new Bundle();
//        bundle.putString(ShenHeFragment.Channels, tabNameList.get(4).getStatus());
//        bundle.putString(ShenHeFragment.t_id, taskId);
//        shenShuFragment.setArguments(bundle);
//        newsFragmentList.add(shenShuFragment);
//        channelTab.addTab(channelTab.newTab().setText(tabNameList.get(4).getTitle()));

        ChannelsAdapter adapter = new ChannelsAdapter(getSupportFragmentManager(), tabNameList, newsFragmentList);
        viewpger.setAdapter(adapter);
        channelTab.setupWithViewPager(viewpger);
        viewpger.setOffscreenPageLimit(tabNameList.size());

        viewpger.setCurrentItem(1);
    }

    @OnClick({R.id.iv_back, R.id.tv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_right:
                showUrl();
                break;
        }
    }

    /**
     * 获取下载图片的地址
     */
    private void showUrl() {
        showProgress();
        taskPresenter.showUrl(taskId, ConstantValue.RequestKey.shenhe_url);
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        dismissProgress();
        if (ConstantValue.RequestKey.shenhe_url.equals(actionType)) {
            String url = (String) data;
            Logger.e("xuke", "url=" + url);

            DownloadDialog loginOutDialog = DownloadDialog.getInstance(url);
            loginOutDialog.show(getSupportFragmentManager(), "download");
            loginOutDialog.setClickCallback(new DownloadDialog.OnClickCallback() {
                @Override
                public void cancel() {
                    //取消
                    loginOutDialog.dismiss();
                }

                @Override
                public void confirm() {
                    //复制
                    copyString(url);
                    loginOutDialog.dismiss();
                }
            });

        }
    }

    /**
     * 复制
     */
    private void copyString(String content) {
        ClipboardManager cm = (ClipboardManager) this.getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData mClipData = ClipData.newPlainText("Label", content);
        if (cm != null) {
            cm.setPrimaryClip(mClipData);
        }
        ToastUtils.showShortToastSafe("复制成功");
    }

    @Override
    public void onFailed(String code, String msg) {
        dismissProgress();
    }
}
